package com.moose.service.sys.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moose.entity.sys.SysUser;
import com.moose.enums.ExceptionEnum;
import com.moose.mapper.sys.SysUserMapper;
import com.moose.security.userdetails.UserDetailsServiceExpansion;
import com.moose.service.sys.SysUserService;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 唐国翔
 * @since 2019-02-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsServiceExpansion {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public UserDetails loadUserByUsernameMobile(String mobile) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.findByUserMobile(mobile);
        if (sysUser == null) {
            throw new BadCredentialsException(ExceptionEnum.user_not_exist.getMessage());
        }
        return sysUser;

    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.findByUserName(username);
        if (sysUser == null) {
            throw new BadCredentialsException(ExceptionEnum.user_not_exist.getMessage());
        }
        return sysUser;
    }
}
